﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using Microsoft.VisualStudio.Shell;
using System.IO;
using System.Windows.Forms;
using System.Collections.Specialized;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;

namespace CnGine.CodeGenerator.Help
{
    public class DTEHelper
    {
        /// <summary>
        /// 获取当前DTE实例
        /// </summary>
        /// <returns></returns>
        public static DTE GetDTEInstance()
        {
            var dte = (DTE)Package.GetGlobalService(typeof(DTE));
            return dte;
        }
        /// <summary>
        /// 获取当前活跃的项目
        /// </summary>
        /// <returns>EnvDTE.Project</returns>
        public static Project GetActiveProject()
        {
            Project project = null;
            var dte = GetDTEInstance();
            if (dte != null)
            {
                var activeProjects = (Array)dte.ActiveSolutionProjects;
                if (activeProjects != null && activeProjects.Length > 0)
                {
                    project = activeProjects.GetValue(0) as Project;
                }
            }
            return project;
        }
        public static SelectedItem GetSelectedItem()
        {
            DTE dte = GetDTEInstance();
            SelectedItems items = dte.SelectedItems;
            if (items != null && items.Count == 1)
            {
                SelectedItem item = items.Item(1);
                return item;
            }
            return null;
        }
        /// <summary>
        /// 获取当前项目的文件路径
        /// </summary>
        /// <returns></returns>
        public static string GetProjectPath()
        {
            Project project = GetActiveProject();
            if (project != null)
            {
                string projPath = project.Properties.Item("FullPath").Value.ToString();
                //获取当前选中项的路径 并和当前项目路径进行合并
                string itemPath = "";
                SelectedItem si = GetSelectedItem();
                if (si != null && si.ProjectItem != null)
                    itemPath = si.ProjectItem.Properties.Item("FullPath").Value.ToString();
                //合并路径
                projPath = Path.Combine(projPath, itemPath);
                //获取合并路径中的文件夹路径
                projPath = System.IO.Path.GetDirectoryName(projPath);
                return projPath;
            }
            else
            {
                throw new Exception("加载项目信息出错，请打开项目后进行尝试...");
            }
        }

        public static void AddFileToProject(SelectedItem selectedItem, StringCollection files)
        {
            ProjectItems projectItems = null;
            if (selectedItem.Project != null && selectedItem.ProjectItem == null)
            {
                projectItems = selectedItem.Project.ProjectItems;
            }
            if (selectedItem.Project == null && selectedItem.ProjectItem != null)
            {
                projectItems = selectedItem.ProjectItem.ProjectItems;
            }
            if (projectItems != null)
            {
                ProjectItem lastItem = null;
                foreach (var file in files)
                {
                    lastItem = projectItems.AddFromFile(file);
                    //lastItem = projectItems.AddFromFileCopy(file);

                }
                if (lastItem != null)
                {
                    DTEHelper.GetDTEInstance().ItemOperations.OpenFile(lastItem.get_FileNames(1), EnvDTE.Constants.vsViewKindPrimary);
                    //lastItem.Open(Constants.vsViewKindPrimary);
                }
            }
        }

        #region OutputWindow
        /// <summary>
        /// 向Output Window 写入信息
        /// </summary>
        /// <param name="Message"></param>
        public static void OutPutMessage(string Message)
        {
            OutPutMessage(Message, false);
        }
        /// <summary>
        /// 向Output Window 写入信息
        /// </summary>
        /// <param name="message">Message</param>
        /// <param name="IsClear">IsClear</param>
        public static void OutPutMessage(string Message, bool IsClear)
        {
            IVsOutputWindow outWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
            //Guid generalWindowGuid = VSConstants.GUID_OutWindowGeneralPane;
            //IVsOutputWindowPane windowPane;
            //outWindow.GetPane(ref generalWindowGuid, out windowPane);
            Guid guidOutPut = new Guid();

            outWindow.CreatePane(ref guidOutPut, "CodeGenerator", 1, 0);
            IVsOutputWindowPane windowPane;
            outWindow.GetPane(ref guidOutPut, out windowPane);

            if (IsClear) windowPane.Clear();
            //激活窗体  ？有问题
            windowPane.Activate();
            windowPane.OutputString(Message + "\n");

        }
        #endregion
    }
}
